﻿using System;
using System.Collections;
using System.Collections.Generic;
using GameProtocols;
using Framework;
using TrueSync;

public class FightGamePlayState : IGameState
{
    FightGame mGame;
    FP GameTime;

    public void Enter(IGameStateMachine game, params object[] param)
    {
        mGame = (FightGame)game;
        GameTime = 60;
    }

    public void Execute(PKG_FRAME frame)
    {
        if (mGame.myPlayer.IsValid == false )
            mGame.ChangeState(new FightGameFinishState());

        GameTime -= LockstepEngine.FrameDeltaTime;
        if(GameTime < 0)//时间到或者一方死亡进入结束状态
        {
            mGame.ChangeState(new FightGameFinishState());
        }
        else if (mGame.myPlayer.handle.GetLogicComponent<LogicStateMachineComponent>().CurrentState is DeadState)
        {
            mGame.ChangeState(new FightGameFinishState());
        }

        foreach (var aoperation in frame.operations)
        {
            ObjHandle<LogicEntity> entity = ObjHandle<LogicEntity>.NULL;
            long playerId = aoperation.playerId;
            
            if(playerId == mGame.myPlayer.handle.PlayerID)
            {
                entity = mGame.myPlayer;
            }
           
            if(entity.IsValid)
            {
                entity.handle.GetLogicComponent<LogicInputComponent>().HandleInput(aoperation.operation);
            }
        }

        mGame.CollisionWorld.Collide();
    }

    public void Exit()
    {
        
    }

    bool hasGameFinished()
    {
        if (mGame.myPlayer.IsValid == false)
            return true;
        if (mGame.myPlayer.handle.GetLogicComponent<LogicStateMachineComponent>().CurrentState is DeadState)
            return true;
        return false;
    }
}
